package com.sixth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.sixth.bean.MarketIssue;
import com.sixth.bean.MarketIssueExample;
import com.sixth.bean.bo.IssueCreateBO;
import com.sixth.bean.bo.IssueListBO;
import com.sixth.bean.bo.IssueBO;
import com.sixth.bean.common.BasePageInfoVo;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.IssueListInnerVO;
import com.sixth.bean.vo.IssueListVO;
import com.sixth.constant.Constant;
import com.sixth.mapper.MarketIssueMapper;
import com.sixth.service.AdminIssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author ljh
 * @version 1.0
 * @describition:
 * @since 2022/9/6 22:59
 **/

@Service
public class AdminIssueServiceImpl implements AdminIssueService {

    @Autowired
    MarketIssueMapper marketIssueMapper;

    @Override
    public IssueListVO list(IssueListBO issueListBO) {
        PageHelper.startPage(issueListBO.getPage(), issueListBO.getLimit());

        MarketIssueExample marketIssueExample = new MarketIssueExample();
        marketIssueExample.setOrderByClause(issueListBO.getSort()+" "+ issueListBO.getOrder());
        MarketIssueExample.Criteria criteria = marketIssueExample.createCriteria();
        criteria.andDeletedEqualTo(Constant.EXIST);
        if(!StringUtil.isEmpty(issueListBO.getQuestion())){
            criteria.andQuestionLike("%"+ issueListBO.getQuestion() + "%");
        }
        List<IssueListInnerVO> issueList = marketIssueMapper.selectByExampleMyself(marketIssueExample);

        PageInfo<IssueListInnerVO> pageInfo = new PageInfo<>(issueList);

        //响应页面信息以及total
        IssueListVO issueListVO = new IssueListVO();
        issueListVO.setPages(pageInfo.getPages());
        issueListVO.setTotal(pageInfo.getTotal());
        issueListVO.setLimit(pageInfo.getPageSize());
        issueListVO.setPage(pageInfo.getPageNum());

        //响应details
        issueListVO.setList(issueList);

        return issueListVO;
    }



    public BasePageInfoVo get(Object o){
        List<Object> resources = (List<Object>) o;
        PageInfo<Object> objectPageInfo = new PageInfo<>(resources);

        BasePageInfoVo<Object> vo = new BasePageInfoVo<>();
        vo.setPages(objectPageInfo.getPages());
        vo.setTotal(objectPageInfo.getTotal());
        vo.setLimit(objectPageInfo.getPageSize());
        vo.setPage(objectPageInfo.getPageNum());


        return vo;
    }


    @Override
    @Transactional
    public MarketIssue create(IssueCreateBO issueCreateBO) {
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setQuestion(issueCreateBO.getQuestion());
        marketIssue.setAnswer(issueCreateBO.getAnswer());

        Date addTime = new Date();
        marketIssue.setAddTime(addTime);
        marketIssue.setUpdateTime(addTime);

        marketIssueMapper.insertInCreate(marketIssue);

        return marketIssue;
    }

    @Transactional
    @Override
    public MarketIssue update(IssueBO issueBO) {

        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setId(issueBO.getId());
        marketIssue.setAddTime(issueBO.getAddTime());
        marketIssue.setUpdateTime(new Date());
        marketIssue.setAnswer(issueBO.getAnswer());
        marketIssue.setQuestion(issueBO.getAnswer());
        marketIssue.setDeleted(issueBO.getDeleted());

        int code = marketIssueMapper.updateByPrimaryKeySelective(marketIssue);
        if(code == 0){
            return null;
        }
        return marketIssue;
    }

    @Transactional
    @Override
    public int delete(Integer id) {

        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setId(id);
        marketIssue.setUpdateTime(new Date());
        marketIssue.setDeleted(true);

        int code = marketIssueMapper.updateByPrimaryKeySelective(marketIssue);
        if(code == 0){
            return 404;
        }

        return 200;
    }
}
